/*
 * News modules
 * author : DUCDQ
 */

 $(function(){
 	//change color in comment row
    $("table#show-table tr:nth-child(odd)").addClass("striped");
    	
	$("#bulk").change(function(e){
		if($("#bulk").val() == 'status')
			$("#changeStatus").show();
		else
			$("#changeStatus").hide();
	});
	
	//effect list.phtml
 	$("input:checkbox[value='checkall']").click (function(){
    	if ($(this).attr("checked") )
    		//check all the things in name=category[]
    		$("input:checkbox[name='category\[\]']").attr("checked","checked");
    });
    
    $("input:checkbox[value='uncheckall']").click (function(){
	if ($(this).attr("checked") )
	{
		//check all the things in name=category[]
		$("input:checkbox[name='category\[\]']").removeAttr("checked");
		$("input:checkbox[value='checkall']").removeAttr("checked");
		$(this).removeAttr("checked");
	}
    });
    
    //bulk action
	$("#bulk-news-form").ajaxForm({
		dataType:  'json',
		success: function(json){
			if(json.bulkaction == 'delete')
			{	
				$.each(json.ids, function(i,id){
		            $("#news-row-" + id).css('background-color','red').fadeOut(2000).remove();
		            console.log("Removing news-box-" + id);	
     			});
    		}
    		else if(json.success == '1')
    		{
   			 	if(json.bulkaction == 'status')
       			{
       				if(json.status == 'deleted' || json.status == 'spam'){
       					$.each(json.ids, function(i,id){
  				            $("#news-row-" + id).css('background-color','blue').fadeOut(2000);
  				            $(".newsIdClass:checkbox:checked").click();
		            	});//end each
		        	}
			        else
			        {
			        	$.each(json.ids, function(i,id){
			            	$(".newsIdClass:checkbox:checked").click();
			            	$("#news-status-" + id).html(json.status);
			           	});
			        }
	     		}
       			else
       				alert('fall');
  			}
   			//set list null
   			json.ids = null;
   		}
	});
	
	//delete href
	$("[id^=delete-news-]").click(function(e){
		var del=confirm("Do you want to delete this news?");
   		deleteId = this.id.split('-')[2];
   		if (del){	
   			$.getJSON("/en/news/ajax/delete-news", {'id': deleteId},
            	function(json) {
                	if(json.success)	
                   		$("#news-row-" + deleteId).css('background-color','red').fadeOut(2000);
                   	else
                    	$("#delete-news-" + deleteId).show().html("Could not delete");
                   return false;
   			});
   		}else
   			return false;
	});

 });